<?php
class LRApi{
	
	var $id = "";
	var $apiName = "";
	var $authToken = "";
	var $lrAccount = "";

	function LRApi($lrAccount, $apiName, $secretWord)
	{
		$this->id = $this->generateId();
		$this->apiName = $apiName;
		$this->authToken = $this->createAuthToken($secretWord);
		$this->lrAccount = $lrAccount;
	}

	function generateId()
	{
		return time().rand(0,9999);
	}

	function createAuthToken($secWord)
	{
		$datePart = gmdate("Ymd");
		$timePart = gmdate("H");

		$authString = $secWord.":".$datePart.":".$timePart;

		require_once ("./sha256.php");

		$sha256 = hash('sha256', $authString);

		return strtoupper($sha256);
	}

	function AuthXml()
	{
		return "<Auth><ApiName>" . $this->apiName . "</ApiName><Token>" . $this->authToken . "</Token></Auth>";
	}

	function getResponse($url, $xml)
	{
		$url .= urlencode($xml);

		if(!function_exists('curl_init'))
		{
			die("Curl library not installed.");
			return "";
		}

		$agents = array();
		$agents[] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0)";
		$agents[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
		$agents[] = "Opera/9.63 (Windows NT 6.0; U; ru) Presto/2.1.1";
		$agents[] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
		$agents[] = "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.18) Gecko/20081203 Firefox/2.0.0.18";
		$agents[] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16";
		$agents[] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";

		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_HEADER, 0);
		curl_setopt ($ch, CURLOPT_USERAGENT, $agents[rand(0,(count($agents)-1))]);
		ob_start();

		curl_exec ($ch);
		curl_close ($ch);
		$content = ob_get_contents();
		ob_end_clean();

		return $content;
	}
}
